local wushuang = fk.CreateSkill {

  name = "joyex__wushuang",

  tags = { Skill.Compulsory, },

}



wushuang:addEffect(fk.TargetSpecified, {
  name = "joyex__wushuang",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.TargetSpecified, fk.TargetConfirmed,fk.AfterCardTargetDeclared},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wushuang.name) then
      return false
    end
    if event == fk.TargetSpecified then
      return target == player and table.contains({ "slash", "duel" }, data.card.trueName)
    elseif event ==  fk.TargetConfirmed then
      return data.to == player.id and data.card.trueName == "duel"
    elseif event == fk.AfterCardTargetDeclared and data.card.trueName == "slash" and target == player then
      local current_targets = data:getAllTargets()
      for _, p in ipairs(player.room.alive_players) do
        if not table.contains(current_targets, p.id) and not player:isProhibited(p, data.card)  and
            data.card.skill:modTargetFilter(p.id, current_targets, data.from, data.card, true) then
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      local current_targets = data:getAllTargets()
      local targets = {}
      for _, p in ipairs(room.alive_players) do
        if not table.contains(current_targets, p.id) and not player:isProhibited(p, data.card) and
            data.card.skill:modTargetFilter(p.id, current_targets, data.from, data.card, true) then
          table.insert(targets, p.id)
        end
      end
      local tos = room:askForChoosePlayers(player, targets, 1, 1,
      "#joyex__wushuang-choose:::"..data.card:toLogString(), wushuang.name, true)
      if #tos > 0 then
        event:setCostData(self, tos)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      table.insertTable(data.tos, table.map(event:getCostData(self), function (p)
        return {p}
      end))
    else
      data.fixedResponseTimes = data.fixedResponseTimes or {}
      if data.card.trueName == "slash" then
        data.fixedResponseTimes["jink"] = 2
      else
        data.fixedResponseTimes["slash"] = 2
        data.fixedAddTimesResponsors = data.fixedAddTimesResponsors or {}
        table.insert(data.fixedAddTimesResponsors, (event == fk.TargetSpecified and data.to or data.from))
      end
    end
  end,
})
wushuang:addEffect(fk.TargetConfirmed, {
  name = "joyex__wushuang",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.TargetSpecified, fk.TargetConfirmed,fk.AfterCardTargetDeclared},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wushuang.name) then
      return false
    end
    if event == fk.TargetSpecified then
      return target == player and table.contains({ "slash", "duel" }, data.card.trueName)
    elseif event ==  fk.TargetConfirmed then
      return data.to == player.id and data.card.trueName == "duel"
    elseif event == fk.AfterCardTargetDeclared and data.card.trueName == "slash" and target == player then
      local current_targets = data:getAllTargets()
      for _, p in ipairs(player.room.alive_players) do
        if not table.contains(current_targets, p.id) and not player:isProhibited(p, data.card)  and
            data.card.skill:modTargetFilter(p.id, current_targets, data.from, data.card, true) then
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      local current_targets = data:getAllTargets()
      local targets = {}
      for _, p in ipairs(room.alive_players) do
        if not table.contains(current_targets, p.id) and not player:isProhibited(p, data.card) and
            data.card.skill:modTargetFilter(p.id, current_targets, data.from, data.card, true) then
          table.insert(targets, p.id)
        end
      end
      local tos = room:askForChoosePlayers(player, targets, 1, 1,
      "#joyex__wushuang-choose:::"..data.card:toLogString(), wushuang.name, true)
      if #tos > 0 then
        event:setCostData(self, tos)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      table.insertTable(data.tos, table.map(event:getCostData(self), function (p)
        return {p}
      end))
    else
      data.fixedResponseTimes = data.fixedResponseTimes or {}
      if data.card.trueName == "slash" then
        data.fixedResponseTimes["jink"] = 2
      else
        data.fixedResponseTimes["slash"] = 2
        data.fixedAddTimesResponsors = data.fixedAddTimesResponsors or {}
        table.insert(data.fixedAddTimesResponsors, (event == fk.TargetSpecified and data.to or data.from))
      end
    end
  end,
})
wushuang:addEffect(fk.AfterCardTargetDeclared, {
  name = "joyex__wushuang",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.TargetSpecified, fk.TargetConfirmed,fk.AfterCardTargetDeclared},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wushuang.name) then
      return false
    end
    if event == fk.TargetSpecified then
      return target == player and table.contains({ "slash", "duel" }, data.card.trueName)
    elseif event ==  fk.TargetConfirmed then
      return data.to == player.id and data.card.trueName == "duel"
    elseif event == fk.AfterCardTargetDeclared and data.card.trueName == "slash" and target == player then
      local current_targets = data:getAllTargets()
      for _, p in ipairs(player.room.alive_players) do
        if not table.contains(current_targets, p.id) and not player:isProhibited(p, data.card)  and
            data.card.skill:modTargetFilter(p.id, current_targets, data.from, data.card, true) then
          return true
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      local current_targets = data:getAllTargets()
      local targets = {}
      for _, p in ipairs(room.alive_players) do
        if not table.contains(current_targets, p.id) and not player:isProhibited(p, data.card) and
            data.card.skill:modTargetFilter(p.id, current_targets, data.from, data.card, true) then
          table.insert(targets, p.id)
        end
      end
      local tos = room:askForChoosePlayers(player, targets, 1, 1,
      "#joyex__wushuang-choose:::"..data.card:toLogString(), wushuang.name, true)
      if #tos > 0 then
        event:setCostData(self, tos)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    if event == fk.AfterCardTargetDeclared then
      table.insertTable(data.tos, table.map(event:getCostData(self), function (p)
        return {p}
      end))
    else
      data.fixedResponseTimes = data.fixedResponseTimes or {}
      if data.card.trueName == "slash" then
        data.fixedResponseTimes["jink"] = 2
      else
        data.fixedResponseTimes["slash"] = 2
        data.fixedAddTimesResponsors = data.fixedAddTimesResponsors or {}
        table.insert(data.fixedAddTimesResponsors, (event == fk.TargetSpecified and data.to or data.from))
      end
    end
  end,
})

return wushuang